package com.kun.rental.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kun.rental.entity.RentalType;
import com.kun.rental.mapper.RentalTypeMapper;
import com.kun.rental.service.IRentalTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ikun
 * @since 2024-07-04
 */
@Service
public class RentalTypeServiceImpl extends ServiceImpl<RentalTypeMapper, RentalType> implements IRentalTypeService {


    @Autowired
    private RentalTypeMapper rentalTypeMapper;

    /**
     * 分页查询
     * @param page
     * @param rentalType
     * @return
     */
    @Override
    public Page<RentalType> searchByPage(Page<RentalType> page, RentalType rentalType) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like(StrUtil.isNotEmpty(rentalType.getTypeName()),
                "type_name", rentalType.getTypeName());
        queryWrapper.le(ObjectUtils.isNotEmpty(rentalType.getHighDiscount()), "type_discount",
                rentalType.getHighDiscount());
        queryWrapper.ge(ObjectUtils.isNotEmpty(rentalType.getLowDiscount()), "type_discount",
                rentalType.getLowDiscount());
        return rentalTypeMapper.selectPage(page, queryWrapper) ;
    }


    /**
     * 删除
     * @param ids
     * @return
     */
    @Override
    public boolean delete(String ids) {
        List<Integer> list = Arrays.stream(ids.split(",")).map(Integer::parseInt).toList();
        if (!list.isEmpty()){
            return baseMapper.deleteBatchIds(list) > 0;
        }
        return false;
    }
}
